def prov(number):
    c = 1
    for i in range(1, int(number**0.5)+1):
        if number%i == 0:
            c+=1
            if c == 3:
                return 0
    return 1
n = int(input())
if n//1000 != 0 and n//10000 == 0:
    if prov(n) == 1:
        print(n)
        quit()
    max_pros_num = -1
    n1 = n
    listwithprosnumbe = []
    while n1:
        if prov(n1%10) == 1:
            listwithprosnumbe.append(n1%10)
        n1//=10
    if prov(n//1000*10+n%1000//100) == 1: listwithprosnumbe.append(n//1000*10+n%1000//100)
    if prov(n%10 + n%100//10*10) == 1: listwithprosnumbe.append(n%10 + n%100//10*10)
    if prov((n-n%10)//10) == 1: listwithprosnumbe.append((n-n%10)//10)
    if prov(n-n//1000*1000) == 1: listwithprosnumbe.append(n-n//1000*1000)
    if prov(n%1000//100*10+n//10%10) == 1: listwithprosnumbe.append(n%1000//100*10+n//10%10)
    if len(listwithprosnumbe) == 0:
        print(0)
    else:
        for i in range(len(listwithprosnumbe)):
            if listwithprosnumbe[i] > max_pros_num:
                max_pros_num = listwithprosnumbe[i]
        print(max_pros_num)
else:
    print(0)
